<?php 
/**
 * Crud Controller 
 * Controller that implements the basic CRUD functions
 *  
 * @package    Lazy Framework
 * @subpackage controller
 * @author     Eduardo Marcolino <eduardo.marcolino@gmail.com>
 * @copyright  (c) 2010 Eduardo Marcolino <eduardo.marcolino@gmail.com>
 */
abstract class CrudController extends Controller {
  
  abstract public function getClass();
  abstract public function getRedirectUrl();
  abstract public function doSave();
  
  public function createOrUpdate() 
  {
    $className = $this->getClass();
    return $this->getRequestParameter('id') == '' 
                    ? new $className() 
                    : $this->getOutletInstance()->load(
                            $this->getClass(),$this->getRequestParameter('id')); 
  }
  
  public function doList() 
  {
    $this->setAttribute('list',$this->getOutletInstance()->from($this->getClass())->find());
    $this->setTemplate($this->getTemplateFolder().'/list.html');
  }
  public function doEdit() 
  {
    $object = $this->getOutletInstance()->load($this->getClass(),$this->getRequestParameter('id'));
    $this->setAttribute('object',$object);
    $this->setTemplate($this->getTemplateFolder().'/edit.html');
  }
  public function doCreate() 
  {
    $this->setTemplate($this->getTemplateFolder().'/new.html');    
  }
  public function doDelete() 
  {
    $this->getOutletInstance()->delete($this->getClass(),$this->getRequestParameter('id'));
    header('location: '.$this->getRedirectUrl());
  }
  protected function getTemplateFolder() {
    return strtolower($this->getClass());
  }
}

?>